﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Security;

namespace CarBlackBox
{
    public partial class Form_UserManage : Form
    {
        public Form_UserManage()
        {
            InitializeComponent();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form_UserEditor f = new Form_UserEditor(Form_Main.Operator);
            if (f.ShowDialog() == DialogResult.OK)
            {
                bindingSource1.Insert(0, f.User);
                dgv.Rows[0].Selected = true;
            }
        }        

        private void Form_UserManage_Load(object sender, EventArgs e)
        {
            foreach (MembershipUser user in Membership.GetAllUsers())
            {
                bindingSource1.Add(user);
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            bindingSource1.Clear();
            if (string.IsNullOrEmpty(toolStripComboBox2.Text))
            {
                foreach (MembershipUser user in Membership.GetAllUsers())
                {
                    bindingSource1.Add(user);
                }
            }
            else
            {
                MembershipUser user = Membership.GetUser(toolStripComboBox2.Text);
                if (user != null)
                    bindingSource1.Add(user);
            }            
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
             if (dgv.SelectedRows.Count == 0)
                 return;
             int index = dgv.SelectedRows[0].Index;
             string UserName = dgv.Rows[index].Cells["UserName"].Value.ToString();
             Form_UserEditor f = new Form_UserEditor(Form_Main.Operator,UserName);
             if (f.ShowDialog() == DialogResult.OK)
             {
                 //toolStripComboBox2.Text = "";
                 //toolStripButton2_Click(null, null);
                 bindingSource1.RemoveAt(index);
                 bindingSource1.Insert(index, f.User);
                 dgv.Rows[index].Selected = true;
             }
        }

        private void dgv_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && e.RowIndex >= 0)//左键双击
            {
                string UserName = dgv.Rows[e.RowIndex].Cells["UserName"].Value.ToString();
                Form_UserEditor f = new Form_UserEditor(Form_Main.Operator, UserName);
                if (f.ShowDialog() == DialogResult.OK)
                {
                    //toolStripComboBox2.Text = "";
                    //toolStripButton2_Click(null, null);
                    bindingSource1.RemoveAt(e.RowIndex);
                    bindingSource1.Insert(e.RowIndex, f.User);
                    dgv.Rows[e.RowIndex].Selected = true;
                }
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (dgv.SelectedRows.Count == 0)
                return;
            if (WFNetLib.WFGlobal.MessageYseOrNo("确认删除当前的选中的用户么？", "删除") == DialogResult.No)
                return;
            int index = dgv.SelectedRows[0].Index;
            string UserName = dgv.Rows[index].Cells["UserName"].Value.ToString();
            Roles.RemoveUserFromRoles(UserName, Roles.GetRolesForUser(UserName));
            Membership.DeleteUser(UserName);
            bindingSource1.RemoveAt(index);
            MessageBox.Show("删除成功");
            WFNetLib.Log.SystemLogData log = new WFNetLib.Log.SystemLogData();
            log.LogType = LogType.用户管理.ToString();
            log.LogContent = "删除用户名为" + UserName + "的用户";
            log.LogRemark = "用户删除";
            log.InserSQLLog();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
